// var SerialPort = require('serialport')
// const serialPort = new SerialPort('/dev/ttyUSB0', { baudRate: 9600, dataBits: 8, stopBits: 1, parity: 'none', autoOpen: false })
var events = require('events');
var emitter = new events.EventEmitter();
var serialPort=require('./config').getSerialPort()
var openSerial = function () {
    serialPort.open((err) => {
        if (err) {
            console.log('failed to open: ' + err);
        } else {
            console.log('open serialport');
            serialPort.on('data', (data) => {
                emitter.emit('data', data);
            })
        }
    })
}

var sendMsg = function (sendBuffer) {
    serialPort.write(sendBuffer, (err) => {
        if (err)
            console.log('err ' + err);
    });
}

module.exports.recvMsg = emitter;
module.exports.sendMsg = sendMsg;
module.exports.openSerial = openSerial;

